package com.ssy.controller;

import cn.hutool.core.util.StrUtil;
import com.ssy.common.Result;
import com.ssy.service.ClassService;
import com.ssy.service.impl.ClassServiceImpl;
import com.ssy.util.RespUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

/**
 * ClassServlet
 *
 * @author ssy
 * @date 2020/11/18
 **/
@WebServlet(urlPatterns = {"/class.do"})
public class ClassServlet extends HttpServlet {

    private static final long serialVersionUID = 3993165212231983774L;

    ClassService classService = new ClassServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");
        if ("classList".equals(type)) {
            classList(req, resp);
        } else if ("addClass".equals(type)) {
            addClass(req, resp);
        } else if ("updateClass".equals(type)) {
            updateClass(req, resp);
        } else if ("bathDelClass".equals(type)) {
            bathDelClass(req, resp);
        } else if ("delOneClass".equals(type)) {
            delOneClass(req, resp);
        } else if ("queryStudentList".equals(type)) {
            queryStudentList(req, resp);
        }
    }

    /**
     * 根据班级ID 加载学生列表
     *
     * @param req
     * @param resp
     */
    private void queryStudentList(HttpServletRequest req, HttpServletResponse resp) {
        String cId = req.getParameter("cId");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");

        HashMap<String, Object> param = new HashMap<>();
        if (StrUtil.isNotBlank(cId)) {
            param.put("cId", cId);
        }
        Result rs = this.classService.selectStudentList(param, page, limit);
        RespUtil.write(resp, rs);
    }

    /**
     * 单个删除班级
     *
     * @param req
     * @param resp
     */
    private void delOneClass(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        Result result = this.classService.deleteOneClass(id);
        RespUtil.write(resp, result);
    }

    /**
     * 批量删除班级
     *
     * @param req
     * @param resp
     */
    private void bathDelClass(HttpServletRequest req, HttpServletResponse resp) {
        String[] ids = req.getParameterValues("id");
        Result result = this.classService.BathdeleteClass(ids);
        RespUtil.write(resp, result);

    }

    /**
     * 修改班级
     *
     * @param req
     * @param resp
     */
    private void updateClass(HttpServletRequest req, HttpServletResponse resp) {
        String cId = req.getParameter("cId");
        String number = req.getParameter("number");
        String cname = req.getParameter("cname");
        String inTime = req.getParameter("inTime");
        String majorId = req.getParameter("majorId");
        String sum = req.getParameter("sum");
        String instructor = req.getParameter("instructor");
        Result rs = this.classService.updateClass1(cId, number, cname, inTime, majorId, sum, instructor);
        RespUtil.write(resp, rs);
    }

    /**
     * 添加班级
     *
     * @param req
     * @param resp
     */
    private void addClass(HttpServletRequest req, HttpServletResponse resp) {
        String number = req.getParameter("number");
        String cname = req.getParameter("cname");
        String inTime = req.getParameter("inTime");
        String majorId = req.getParameter("majorId");
        String sum = req.getParameter("sum");
        String instructor = req.getParameter("instructor");
        Result rs = this.classService.addClass(number, cname, inTime, majorId, sum, instructor);
        RespUtil.write(resp, rs);
    }

    /**
     * 班级列表
     *
     * @param req
     * @param resp
     */
    private void classList(HttpServletRequest req, HttpServletResponse resp) {
        String number = req.getParameter("number");
        String cname = req.getParameter("cname");
        String majorId = req.getParameter("majorId");
        String inTime = req.getParameter("inTime");
        String instructor = req.getParameter("instructor");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");

        HashMap<String, Object> param = new HashMap<>();
        if (StrUtil.isNotBlank(number)) {
            param.put("number", number);
        }
        if (StrUtil.isNotBlank(cname)) {
            param.put("cname", cname);
        }
        if (StrUtil.isNotBlank(majorId)) {
            param.put("majorId", majorId);
        }
        if (StrUtil.isNotBlank(inTime)) {
            param.put("inTime", inTime);
        }
        if (StrUtil.isNotBlank(instructor)) {
            param.put("instructor", instructor);
        }
        Result result = this.classService.queryPage(param, page, limit);
        RespUtil.write(resp, result);
    }

}
